/*
 * Copyright (C) 2010 Moduad Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cc.telecomdigital.tdstock.activity.groups.more.push.client;

import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Handler;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;

/**
 * A broadcast receiver to handle the changes in network connectiion states.
 * 
 * @author Sehwan Noh (devnoh@gmail.com)
 */
public class ConnectivityReceiver extends BroadcastReceiver
{

	private static final String	TAG		= ConnectivityReceiver.class
											.getSimpleName();

	private NotificationService	notificationService;
	private Handler			handler	= null;
	private int				number	= 0;

	public ConnectivityReceiver(NotificationService notificationService)
	{
		this.notificationService = notificationService;
	}

	@Override
	public void onReceive(Context context, Intent intent)
	{
		TDStockLog.d(TAG, "ConnectivityReceiver.onReceive()...");
		String action = intent.getAction();
		TDStockLog.d(TAG, "action=" + action);
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

		if (networkInfo != null)
		{
			TDStockLog
					.d(TAG, "Network Type  = " + networkInfo.getTypeName());
			TDStockLog.d(TAG, "Network State = " + networkInfo.toString());

			if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI)
			{

				WifiManager mWifiManager = (WifiManager) context
						.getSystemService(Context.WIFI_SERVICE);
				WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
				TDStockLog.d(TAG, "ssid" + wifiInfo.getSSID() + "强度："
						+ wifiInfo.getLinkSpeed() + "client status"
						+ wifiInfo.getSupplicantState() + "ip"
						+ wifiInfo.getIpAddress());

				/**
				 * 获取连接wifi的相关信息
				 */
				List<ScanResult> list = mWifiManager.getScanResults();
				for (int i = 0; i < list.size(); i++)
				{
					TDStockLog.d(TAG, "ssid" + list.get(i).SSID + "强度："
							+ list.get(i).level);
				}
			}
			else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE)
			{
				// networkInfo.getState()
			}

			if (networkInfo.isConnected())
			{
				TDStockLog.i(TAG, "Network connected");
				XmppManager.waiting = 0;
				/*if (null != notificationService.getXmppManager()
						&& null != notificationService.getXmppManager()
								.getConnection()) {
					Log.d(TAG, "reconnection");*/

				// Log.d(TAG,
				// "Satus="+notificationService.getXmppManager().getStatus());
				// one of connection
				/*
				
				 * ReconnectionThread.flag=false;
				 * Log.d(TAG,"isConnection"+notificationService
				 * .getXmppManager().getConnection().isConnected());
				 * notificationService.connect();
				 * Log.d(TAG,"isConnection"+notificationService
				 * .getXmppManager().getConnection().isConnected());
				 */
				/*
				 * if (notificationService.getXmppManager().getStatus() ==
				 * ConnectionStatus.DEFAULT) { //
				 * notificationService.startService(); } else if
				 * (notificationService.getXmppManager().getStatus() ==
				 * ConnectionStatus.CONN) {
				 * 
				 * } else if
				 * (notificationService.getXmppManager().getStatus() ==
				 * ConnectionStatus.DISCONN) { //XmppManager.flag = false;
				 * notificationService.disconnect();
				 */
				//boolean falg=notificationService.getXmppManager().getConnection().isConnected();
				//Log.d(TAG,"isConnected()="+falg);
				//if(!falg){
				if (number == 0)
				{
					number++;
					return;
				}
				if (handler == null)
				{
					handler = new Handler();
				}
				handler.postDelayed(new Runnable()
				{

					@Override
					public void run()
					{
						// TODO Auto-generated method stub
						notificationService.connect(TAG);
					}
				}, Constants.RECONNECTION_DELAY);
				//}
				//} else {
				//notificationService.startService(TAG);
				//}
			}
			else
			{
				TDStockLog.e(TAG, "Network unavailable");
				//notificationService.disconnect();
			}
		}
	}
}
